<template>
  <div class="payment-container">
    <div class="payment-header">
      <h2>订单支付</h2>
      <p>确认订单信息并完成支付</p>
    </div>

    <div class="payment-content">
      <el-row :gutter="30">
        <el-col :span="16">
          <div class="order-details">
            <h3>订单详情</h3>
            <el-table :data="orderItems" style="width: 100%">
              <el-table-column label="商品名称" prop="dishname"></el-table-column>
              <el-table-column label="单价" width="100">
                <template slot-scope="scope">
                  ¥{{ scope.row.dishprice }}
                </template>
              </el-table-column>
              <el-table-column label="数量" width="100">
                <template slot-scope="scope">
                  {{ scope.row.number }}
                </template>
              </el-table-column>
              <el-table-column label="小计" width="120">
                <template slot-scope="scope">
                  ¥{{ scope.row.dishprice * scope.row.number }}
                </template>
              </el-table-column>
            </el-table>
          </div>

          <div class="delivery-info">
            <h3>配送信息</h3>
            <el-form :model="deliveryForm" :rules="deliveryRules" ref="deliveryForm" label-width="100px">
              <el-form-item label="收货人" prop="receiver">
                <el-input v-model="deliveryForm.receiver" placeholder="请输入收货人姓名"></el-input>
              </el-form-item>
              <el-form-item label="联系电话" prop="phone">
                <el-input v-model="deliveryForm.phone" placeholder="请输入联系电话"></el-input>
              </el-form-item>
              <el-form-item label="收货地址" prop="address">
                <el-input 
                  v-model="deliveryForm.address" 
                  type="textarea" 
                  :rows="3"
                  placeholder="请输入详细收货地址"
                ></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input 
                  v-model="deliveryForm.remark" 
                  type="textarea" 
                  :rows="2"
                  placeholder="请输入备注信息（可选）"
                ></el-input>
              </el-form-item>
            </el-form>
          </div>
        </el-col>

        <el-col :span="8">
          <div class="payment-summary">
            <h3>支付信息</h3>
            <div class="summary-item">
              <span>商品总额:</span>
              <span>¥{{ totalAmount }}</span>
            </div>
            <div class="summary-item">
              <span>配送费:</span>
              <span>¥{{ deliveryFee }}</span>
            </div>
            <div class="summary-item total">
              <span>应付总额:</span>
              <span>¥{{ totalAmount + deliveryFee }}</span>
            </div>

            <div class="payment-methods">
              <h4>选择支付方式</h4>
              <el-radio-group v-model="paymentMethod">
                <el-radio label="alipay">
                  <i class="el-icon-money"></i> 支付宝
                </el-radio>
                <el-radio label="wechat">
                  <i class="el-icon-money"></i> 微信支付
                </el-radio>
                <el-radio label="card">
                  <i class="el-icon-money"></i> 银行卡
                </el-radio>
              </el-radio-group>
            </div>

            <div class="payment-actions">
              <el-button @click="$router.go(-1)" size="large">返回购物车</el-button>
              <el-button 
                type="primary" 
                size="large" 
                @click="processPayment"
                :loading="paying"
              >
                立即支付 ¥{{ totalAmount + deliveryFee }}
              </el-button>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 支付确认对话框 -->
    <el-dialog title="支付确认" :visible.sync="showPaymentDialog" width="400px">
      <div class="payment-confirm">
        <p>确认使用 <strong>{{ getPaymentMethodName() }}</strong> 支付 ¥{{ totalAmount + deliveryFee }} 吗？</p>
        <div class="confirm-actions">
          <el-button @click="showPaymentDialog = false">取消</el-button>
          <el-button type="primary" @click="confirmPayment" :loading="paying">
            确认支付
          </el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'Payment',
  data() {
    return {
      orderItems: [],
      totalAmount: 0,
      deliveryFee: 5,
      deliveryForm: {
        receiver: '',
        phone: '',
        address: '',
        remark: ''
      },
      deliveryRules: {
        receiver: [
          { required: true, message: '请输入收货人姓名', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入收货地址', trigger: 'blur' }
        ]
      },
      paymentMethod: 'alipay',
      paying: false,
      showPaymentDialog: false
    }
  },
  computed: {
    ...mapGetters(['isLoggedIn', 'userInfo'])
  },
  methods: {
    loadOrderData() {
      // 从路由参数获取订单数据
      const total = this.$route.query.total
      const items = this.$route.query.items
      
      if (total && items) {
        this.totalAmount = parseFloat(total)
        this.orderItems = JSON.parse(items)
      } else {
        this.$message.error('订单数据不完整')
        this.$router.push('/cart')
      }
    },
    
    processPayment() {
      this.$refs.deliveryForm.validate((valid) => {
        if (valid) {
          this.showPaymentDialog = true
        } else {
          this.$message.error('请完善配送信息')
        }
      })
    },
    
    async confirmPayment() {
      this.paying = true
      
      try {
        // 模拟支付过程
        await new Promise(resolve => setTimeout(resolve, 2000))
        
        this.$message.success('支付成功！订单已提交')
        
        // 清空购物车（这里应该调用后端接口）
        // await clearCart()
        
        // 跳转到订单成功页面或主页
        this.$router.push('/dashboard')
      } catch (error) {
        this.$message.error('支付失败，请重试')
      } finally {
        this.paying = false
        this.showPaymentDialog = false
      }
    },
    
    getPaymentMethodName() {
      const methods = {
        alipay: '支付宝',
        wechat: '微信支付',
        card: '银行卡'
      }
      return methods[this.paymentMethod] || '支付宝'
    }
  },
  
  mounted() {
    if (!this.isLoggedIn) {
      this.$message.warning('请先登录')
      this.$router.push('/login')
      return
    }
    
    this.loadOrderData()
    
    // 预填充用户信息
    if (this.userInfo) {
      this.deliveryForm.receiver = this.userInfo.username || ''
    }
  }
}
</script>

<style lang="scss" scoped>
.payment-container {
  padding: 20px;
  
  .payment-header {
    text-align: center;
    margin-bottom: 30px;
    
    h2 {
      font-size: 28px;
      color: #333;
      margin-bottom: 10px;
    }
    
    p {
      color: #666;
      font-size: 16px;
    }
  }
  
  .payment-content {
    .order-details, .delivery-info {
      background: #fff;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
      margin-bottom: 20px;
      
      h3 {
        margin: 0 0 20px 0;
        font-size: 18px;
        color: #333;
        border-bottom: 2px solid #409EFF;
        padding-bottom: 10px;
      }
    }
    
    .payment-summary {
      background: #fff;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
      position: sticky;
      top: 20px;
      
      h3 {
        margin: 0 0 20px 0;
        font-size: 18px;
        color: #333;
        border-bottom: 2px solid #409EFF;
        padding-bottom: 10px;
      }
      
      .summary-item {
        display: flex;
        justify-content: space-between;
        margin-bottom: 15px;
        font-size: 16px;
        
        &.total {
          font-size: 18px;
          font-weight: bold;
          color: #e74c3c;
          border-top: 1px solid #eee;
          padding-top: 15px;
        }
      }
      
      .payment-methods {
        margin: 30px 0;
        
        h4 {
          margin: 0 0 15px 0;
          font-size: 16px;
          color: #333;
        }
        
        .el-radio-group {
          display: flex;
          flex-direction: column;
          gap: 15px;
          
          .el-radio {
            margin-right: 0;
            
            i {
              margin-right: 8px;
            }
          }
        }
      }
      
      .payment-actions {
        display: flex;
        flex-direction: column;
        gap: 15px;
        
        .el-button {
          width: 100%;
        }
      }
    }
  }
  
  .payment-confirm {
    text-align: center;
    
    p {
      margin-bottom: 20px;
      font-size: 16px;
    }
    
    .confirm-actions {
      display: flex;
      justify-content: center;
      gap: 15px;
    }
  }
}
</style> 